import Vue from 'vue';
import Router from 'vue-router'

// import 一次性引入组件 （不使用按需引入）
// import Index from '@/views/index'
// import Admin from '@/views/admin'
// import NotFound from '@/views/404'
import Layout from '@/layout/index'

Vue.use(Router)
const commonRouters = [
    
    {
        path: '/admin',
        component: () => import('@/views/admin'),
        // component: Admin,
        meta: {
            title: 'admin'
        }
    },
    {
        path: '/notFound',
        name: 'NotFound',
        // component: NotFound,
        component: () => import('@/views/404'),
        meta: {
            title: 'notFound',
            hidden: true
        },
    }
]
const menuRouters = [
    {
        path: '/',
        component: Layout,
        name: 'Layout',
        redirect: '/index',
        meta: {
            title: 'layout'
        },
        children: [
            {
                path: 'index',
                // name: 'Index',
                component: () => import('@/views/index'),
                // component: Index,
                meta: {
                    title: '首页',
                },
            },
        ]
    },
    {
        path: '/jsPage',
        component: Layout,
        meta: {
            title: 'js页面',
        },
        children: [
            {
                path: 'test',
                component: () => import('@/views/jsPage/01Js'),
                meta: {
                    title: '测试'
                }
            },
            {
                path: 'imgWebpack',
                component: () => import('@/views/jsPage/02ImgWebpack'),
                meta: {
                    title: '小图'
                }
            },
            {
                path: 'request',
                component: () => import('@/views/jsPage/03Request'),
                meta: {
                    title: '网络请求'
                }
            },
        ]
    },
    {
        path: '/otherPage',
        component: Layout,
        meta: {
            title: 'other页面'
        },
        children: [
            {
                path: 'lazyImg',
                // name: 'LazyImg',
                component: () => import('@/views/lazyImg'),
                meta: {
                    title: '图片懒加载'
                },
            },
            {
                path: 'table',
                component: () => import('@/views/otherPage/01Table'),
                meta: {
                    title: '表格'
                }
            },
            {
                path: 'componentLive',
                component: () => import('@/views/otherPage/02ComponentLive'),
                meta: {
                    title: '组件生命周期'
                },
                beforeEnter(to, from, next){
                    console.log('beforeEnter');
                    next()
                }
            },
            {
                path: 'cookieAndStorage',
                component: () => import('@/views/otherPage/03Storage'),
                meta: {
                    title: '前端存储'
                },
            },
            {
                path: 'tree',
                component: () => import('@/views/otherPage/04Tree'),
                meta: {
                    title: '树'
                },
            },
        ]
    }
]
const routers = [].concat(commonRouters, menuRouters)
routers.push({
    path: '*',
    meta: {
        hidden: true
    },
    beforeEnter: (to, form, next) => {
        next('/notFound')
    }
})
const router = new Router({})
const eachRoute = (routers, pareantPath = '') => {
    routers.forEach(item => {
        item.path = `${pareantPath}/${item.path}`.replace(/^\/+/, '/')
        router.addRoute(item)
        if (item.children) {
            eachRoute(item.children, item.path)
        }
    })
}
eachRoute(routers)
router.beforeEach((to, from, next) => {
    console.log('beforeEach', from, to);
    next()
})
router.beforeResolve((to, from, next) => {
    console.log('beforeResolve');
    next()
})
router.afterEach((to, from) => {
    console.log('afterEach', from, to);
})
export { commonRouters, menuRouters, routers }
export default router